package sol.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * Word Pattern
 */
public class Issue290 {
    public boolean wordPattern(String pattern, String str) {
        Map<Character, String> map = new HashMap<Character, String>();
        String[] words = str.split(" ");
        if (pattern.length()!=words.length)
            return false;
        for (int i = 0; i < pattern.length(); ++i) {
            char c = pattern.charAt(i);
            String word = words[i];
            if (map.containsKey(c)&&map.get(c).equals(word))
                continue;
            if (map.containsKey(c)&&!map.containsValue(word))
                return false;
            if (!map.containsKey(c)&&map.containsValue(word))
                return false;
            map.put(c, word);
        }
        return true;
    }

    public static void main(String[] args) {
        String pattern = "abba";
        String str = "dog cat cat dog";

        Issue290 sol = new Issue290();
        System.out.println(sol.wordPattern(pattern, str));
    }
}
